import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/index.vue'
import store from "../store/index"


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // },
  {
    path: '/Home',
    name: 'Home',
    component: () => import(/* webpackChunkName: "about" */ '../views/Home/index.vue')
  },
  {
    path: '/Categories',
    name: 'Categories',
    meta:{
      auth:true
     },
    component: () => import(/* webpackChunkName: "about" */ '../views/categories/index.vue')
  },
  {
    path: '/Detail',
    name: 'Detail',
    meta:{
     showTab:false
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/detail/index.vue')
  },
  {
    path: '/Cart',
    name: 'Cart',
    meta:{
      auth:true
     },
    component: () => import(/* webpackChunkName: "about" */ '../views/cart/index.vue')
  },
  {
    path: '/Bill',
    name: 'Bill',
    component: () => import(/* webpackChunkName: "about" */ '../views/bill/index.vue')
  },
  {
    path: '/user',
    name: 'user',
    children:[
      {
        path:'login',
        name:'Login',
        meta:{
         auth:false
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/user/Login.vue')
      },
      {
        path:'reg',
        //true 需要登陆才能访问
        meta:{
          auth:false
         },
        component: () => import(/* webpackChunkName: "about" */ '../views/user/Registr.vue')
      },
      {
        path:'',
        component: () => import(/* webpackChunkName: "about" */ '../views/user/User.vue')
      },
    ],
    component: () => import(/* webpackChunkName: "about" */ '../views/user/index.vue')
  },
  {
    path: '/Msg',
    name: 'Msg',
    component: () => import(/* webpackChunkName: "about" */ '../views/msg/index.vue')
  },
  {
    path: '/Service',
    name: 'Service',
    component: () => import(/* webpackChunkName: "about" */ '../views/service/index.vue')
  },
  {
    path: '/Search',
    name: 'Search',
    component: () => import(/* webpackChunkName: "about" */ '../views/search/index.vue')
  },
  {
    path: '/socket',
    name: 'Socket',
    meta:{
      auth:true
     },
    component: () => import(/* webpackChunkName: "about" */ '../views/socket/Socket.vue')
  },

]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//全局的前置守卫--路由的拦截器--路游的生命周期-钩子函数
router.beforeEach((to,from,next)=>{
  //to是要去哪 from来自哪里 next放行
  //next有几种方法：字符串地址，布尔值，对象，无论怎样next必须执行
 var token=store.getters.token 
 var auth=to.meta.auth
 //如果需要登陆，并且登录了，让他进入  如果没有登陆，不需要登陆，让它进去
 if((token&&auth===true)||(!token&&auth===false)){
  next();
  //如果登陆了，不需要登陆才能访问，不烫它进入
 }else if((token&&auth===false)||(!token&&auth===true)){
   next(false)
 }else{
   //不需要判断的给它放行
   next()
 }
  
})
//路由后置守卫
// router.afterEach((to,from)=>{
//   //预先请求数据
//   //上传日志    用户的操作行为，用户那个时间进入这个页面
// })
export default router
